package Service.Teacher.Imp;

import Dao.DaoFactory;
import Dao.Grade.FinalGradeDao;
import Dao.Grade.GroupGradeDao;
import Dao.Grade.GradingDao;
import Dao.Process.AdviserGradeDao;
import Dao.TitleAndPaper.PaperDao;
import Dao.TitleAndPaper.TitleDao;
import Entity.Grade.FinalGrade;
import Entity.Grade.Grading;
import Entity.Grade.GroupGrade;
import Entity.MyTableData;
import Entity.TitleAndPaper.Paper;
import Service.Teacher.GradeService;
import Util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GradeServiceImp implements GradeService {
    private Connection conn;
    private GradingDao gradingDao;
    private PaperDao paperDao;
    private TitleDao titleDao;
    private FinalGradeDao finalGradeDao;
    private GroupGradeDao groupGradeDao;
    private AdviserGradeDao adviserGradeDao;

    public GradeServiceImp() {
        conn = DBUtil.getConnByDruid();
        gradingDao = DaoFactory.getInstance("gradingDao");
        paperDao = DaoFactory.getInstance("paperDao");
        titleDao = DaoFactory.getInstance("titleDao");
        finalGradeDao = DaoFactory.getInstance("finalGradeDao");
        groupGradeDao = DaoFactory.getInstance("groupGradeDao");
        adviserGradeDao = DaoFactory.getInstance("adviserGradeDao");
    }

    /**
     * @param queryKey 论文标题 查询的关键字  为%%查询所有
     * @param offset   页面限制的数据条数大小
     * @param limit    第几条开始
     * @param NO       查询编号
     * @param type     1是学生查询查询 ； 2 是老师查询
     * @return MyTableData
     */
    @Override
    public MyTableData getGradingByNO(String queryKey, Integer offset, Integer limit, String NO, Integer type) {
        List<Grading> grading = gradingDao.getGradingByTNO(queryKey, offset, limit, NO, type, conn);
        if (grading.size() > 0) {
            for (Grading grading1 : grading) {
                //查询关联的论文
                Paper paper = paperDao.getAllPaper("%%", 0, 1, grading1.getSno(), 1, conn).get(0);
                //查询论文关联的课题信息
                paper.setTitle1(titleDao.getTitleById(paper.getXtId(), conn));
                grading1.setPaper(paper);
            }
        }
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", grading.size(), grading);
    }

    /**
     * 评阅老师评分
     *
     * @param grading 要插入的对象
     * @return true 成功   false 失败
     */
    @Override
    public Boolean addGrading(Grading grading) {
        boolean flag = false;
        try {
            int res1, res2;
            conn.setAutoCommit(false);//取消事务自动提交
            res1 = gradingDao.updateGrading(grading, conn);
            Grading newGrading = gradingDao.getGradingByTNO("%%", 0, 1, grading.getSno(), 1, conn).get(0);
            //更新总分数据
            int grade = newGrading.getGrade1() + newGrading.getGrade2() + newGrading.getGrade3();
            FinalGrade finalGrade = new FinalGrade();
            finalGrade.setSno(newGrading.getSno());
            finalGrade.setGrade2(grade);
            finalGrade.setGrade2Id(newGrading.getId());
            res2 = finalGradeDao.upDateGrade(finalGrade, conn);
            if (res1 == 1 && res2 == 1) {  //如果两者的操作结果都是影响满足条件则提交事务
                conn.commit();
                flag = true;
            } else {
                conn.rollback();
            }
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
        }
        return flag;
    }

    /**
     * @param queryKey 论文标题 查询的关键字  为%%查询所有
     * @param offset   页面限制的数据条数大小
     * @param limit    第几条开始
     * @param NO       查询编号
     * @param type     1是学生查询查询 ； 2 是老师查询
     * @return Grading列表
     */
    @Override
    public MyTableData getGroupGrade(String queryKey, Integer offset, Integer limit, String NO, Integer type) {
        List<GroupGrade> groupGrades = groupGradeDao.getAllGroupGradeByNO(queryKey, offset, limit, NO, type, conn);
        if (groupGrades.size() > 0) {
            for (GroupGrade groupGrade : groupGrades) {
                //查询关联的论文
                Paper paper = paperDao.getAllPaper("%%", 0, 1, groupGrade.getSno(), 1, conn).get(0);
                //查询论文关联的课题信息
                paper.setTitle1(titleDao.getTitleById(paper.getXtId(), conn));
                groupGrade.setPaper(paper);
            }
        }
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", groupGrades.size(), groupGrades);
    }

    @Override
    public boolean addGroupGrade(GroupGrade groupGrade) {
        boolean flag = false;
        System.out.println(groupGrade);
        try {
            int res1, res2;
            conn.setAutoCommit(false);//取消事务自动提交
            //往答辩小组的表中插入一条数据
            // 如果是一辩则更新数据
            /*if ("一辩".equals(groupGrade.getReply())) {
                res1 = groupGradeDao.updateGroupGradeByNOAndReply(groupGrade, conn);
            } else if ("二辩".equals(groupGrade.getReply())) {   //如果是二辩则插入数据
                res1 = groupGradeDao.addGroupGrade(groupGrade, conn);
            } else {
                return false;
            }*/
            res1 = groupGradeDao.updateGroupGradeByNOAndReply(groupGrade, conn);
            //获取插入的数据 更新总的评分表答辩小组的评分的数据
            GroupGrade grade = groupGradeDao.getGroupGradeByNOAndReply(groupGrade.getSno(), groupGrade.getReply(), conn);
            FinalGrade finalGrade = finalGradeDao.getFinalBySNO(groupGrade.getSno(), conn);
            finalGrade.setSno(groupGrade.getSno());
            int grade1 = grade.getGrade1() + grade.getGrade2() + grade.getGrade3() + grade.getGrade4();
            finalGrade.setGrade3(grade1);
            finalGrade.setGrade3Id(grade.getId());
            //不同阶段的分数按比例计算  比试这里写死了 有需要可以在数据库设置
            Double finalGradeSQL = finalGrade.getGrade1() * 0.2 + finalGrade.getGrade2() * 0.3 + finalGrade.getGrade3() * 0.5;
            finalGrade.setFinalGrade(finalGradeSQL);
            res2 = finalGradeDao.updateGroupGrade(finalGrade, conn);
            //都更新了才提交事务
            if (res1 == 1 && res2 == 1) {
                conn.commit();
                flag = true;
            } else {
                conn.rollback();
            }
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(conn);
        }
        return flag;
    }

    @Override
    public MyTableData getAllFinalGrade(String queryKey, Integer offset, Integer limit) {
        List<FinalGrade> finalGrades = finalGradeDao.getAllFinalGradeByNO(queryKey, offset, limit, conn);
        if (finalGrades.size() > 0) {
            for (FinalGrade finalGrade : finalGrades) {
                //设置关联的指导老师成绩 评阅老师的成绩 和答辩小组的成绩
                finalGrade.setAdviserGrade(adviserGradeDao.getAdviserGradeBySNO(finalGrade.getSno(), conn));
                finalGrade.setGrading(gradingDao.getGradingByTNO("%%", 0, 1, finalGrade.getSno(), 1, conn).get(0));
                finalGrade.setGroupGrade(groupGradeDao.getAllGroupGradeByNO("%%", 0, 1, finalGrade.getSno(), 1, conn).get(0));
                Paper paper = paperDao.getAllPaper("%%", 0, 1, finalGrade.getSno(), 1, conn).get(0);
                paper.setTitle1(titleDao.getTitleById(paper.getXtId(), conn));
                finalGrade.setPaper(paper);
            }
        }
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", finalGrades.size(), finalGrades);
    }
}
